/*
 * @Description: 
 * @FilePath: /STM32F103/USER/bsp/rcc.c
 * @Author: Wei Zhou
 * @Github: https://github.com/zromyk
 * @Date: 2021-04-28 15:25:19
 * @LastEditors: Wei Zhou
 * @LastEditTime: 2021-05-12 21:51:56
 * @Copyright: Copyright  2017 muyiro. All rights reserved.
 */

#include "rcc.h"

void func_rcc_periph_clock_enable(uint32_t dev_base, uint32_t rcc_periph)
{
    uint32_t base = dev_base & 0xFFFF0000;
    
    switch (base) {
        case APB1PERIPH_BASE: RCC_APB1PeriphClockCmd(rcc_periph, ENABLE); return;
        case APB2PERIPH_BASE: RCC_APB2PeriphClockCmd(rcc_periph, ENABLE); return;
        case AHBPERIPH_BASE:  RCC_AHBPeriphClockCmd(rcc_periph, ENABLE); return;
    }
    assert_param(0); /* error */
}

uint32_t func_rcc_periph_clock_rate(uint32_t dev_base)
{
    uint32_t base = dev_base & 0xFFFF0000;
    RCC_ClocksTypeDef RCC_Clocks;
    RCC_GetClocksFreq(&RCC_Clocks);

    switch (base) {
        case APB1PERIPH_BASE: return RCC_Clocks.PCLK1_Frequency / 1000000 * 2;
        case APB2PERIPH_BASE: return RCC_Clocks.PCLK2_Frequency / 1000000 * 2;
        case AHBPERIPH_BASE:  return RCC_Clocks.HCLK_Frequency  / 1000000;
    }
    assert_param(0); /* error */
    return 72;
}
